package com.ctrip.ec.api.mobile.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by jiang on 2018/4/29.
 */
public class CrossFilter implements Filter {



    public void init(FilterConfig filterConfig) throws ServletException { }



    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        HttpServletRequest request = (HttpServletRequest) servletRequest;

// 指定允许其他域名访问

        response.setHeader("Access-Control-Allow-Origin", "*");

// 响应类型

        response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, OPTIONS, DELETE");

// 响应头设置

        response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header, auth-token");

        if ("OPTIONS".equals(request.getMethod())){

            response.setStatus(response.SC_NO_CONTENT);

        }

        filterChain.doFilter(servletRequest, servletResponse);

    }



    public void destroy() { }

}
